home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / cagd_lib / bzr_wrt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  4.9 KB  |  163 lines

  1. /******************************************************************************
  2. * Bzr-Wrt.c - Bezier handling routines - write to file.                  *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Mar. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "cagd_loc.h"
  11.  
  12. /******************************************************************************
  13. * Writes a bezier curve(s) list into file. Returns TRUE if succesful, FALSE   *
  14. * otherwise.                                      *
  15. * If Comment is NULL, no comment is printed, if "" only internal coment.      *
  16. ******************************************************************************/
  17. int BzrCrvWriteToFile(CagdCrvStruct *Crvs, char *FileName, int Indent,
  18.                          char *Comment,    char **ErrStr)
  19. {
  20.     int i;
  21.     FILE *f;
  22.  
  23.     if ((f = fopen(FileName, "w")) == NULL) {
  24.     *ErrStr = "Fail to open file";
  25.     return FALSE;
  26.     }
  27.     i = BzrCrvWriteToFile2(Crvs, f, Indent, Comment, ErrStr);
  28.  
  29.     fclose(f);
  30.  
  31.     return i;
  32. }
  33.  
  34. /******************************************************************************
  35. * Writes a bezier curve(s) list into file. Returns TRUE if succesful, FALSE   *
  36. * otherwise. The file is not closed.                          *
  37. * If Comment is NULL, no comment is printed, if "" only internal coment.      *
  38. ******************************************************************************/
  39. int BzrCrvWriteToFile2(CagdCrvStruct *Crvs, FILE *f, int Indent, char *Comment,
  40.                                 char **ErrStr)
  41. {
  42.     int i, j, MaxCoord;
  43.  
  44.     if (Comment != NULL) {
  45.     _CagdFprintf(f, Indent, "#\n");
  46.     _CagdFprintf(f, Indent, "# cagd_lib - bezier curve(s) dump.\n");
  47.     _CagdFprintf(f, Indent, "#\n");
  48.     _CagdFprintf(f, Indent, "# %s\n", Comment);
  49.     _CagdFprintf(f, Indent, "#\n");
  50.     }
  51.  
  52.     *ErrStr = NULL;
  53.  
  54.     while (Crvs) {
  55.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crvs -> PType);
  56.  
  57.     if (Crvs -> GType != CAGD_CBEZIER_TYPE) {
  58.         *ErrStr = "Given curve(s) is (are) not BEZIER curve(s)";
  59.         break;
  60.     }
  61.     _CagdFprintf(f, Indent, "[CURVE BEZIER %d %c%c\n",
  62.         Crvs -> Length,
  63.         CAGD_IS_RATIONAL_PT(Crvs -> PType) ? 'P' : 'E',
  64.         MaxCoord + '0');
  65.     Indent += 4;
  66.  
  67.     for (i = 0; i < Crvs -> Length; i++) {
  68.         _CagdFprintf(f, Indent, "[");
  69.         if (CAGD_IS_RATIONAL_PT(Crvs -> PType))
  70.         _CagdFprintf(f, 0, "%s ", _CagdReal2Str(Crvs -> Points[0][i]));
  71.         for (j = 1; j <= MaxCoord; j++) {
  72.         _CagdFprintf(f, 0, "%s", _CagdReal2Str(Crvs -> Points[j][i]));
  73.         if (j < MaxCoord) _CagdFprintf(f, 0, " ");
  74.         }
  75.         _CagdFprintf(f, 0, "]\n");
  76.     }
  77.  
  78.     Indent -= 4;
  79.     _CagdFprintf(f, Indent, "]\n\n");
  80.  
  81.     Crvs = Crvs -> Pnext;
  82.     }
  83.  
  84.     return *ErrStr == NULL;
  85. }
  86.  
  87. /******************************************************************************
  88. * Writes a bezier curve(s) list into file. Returns TRUE if succesful, FALSE   *
  89. * otherwise.                                      *
  90. * If Comment is NULL, no comment is printed, if "" only internal coment.      *
  91. ******************************************************************************/
  92. int BzrSrfWriteToFile(CagdSrfStruct *Srfs, char *FileName, int Indent,
  93.                          char *Comment,    char **ErrStr)
  94. {
  95.     int i;
  96.     FILE *f;
  97.  
  98.     if ((f = fopen(FileName, "w")) == NULL) {
  99.     *ErrStr = "Fail to open file";
  100.     return FALSE;
  101.     }
  102.     i = BzrSrfWriteToFile2(Srfs, f, Indent, Comment, ErrStr);
  103.  
  104.     fclose(f);
  105.  
  106.     return i;
  107. }
  108.  
  109. /******************************************************************************
  110. * Writes a bezier curve(s) list into file. Returns TRUE if succesful, FALSE   *
  111. * otherwise. The file is not closed.                          *
  112. ******************************************************************************/
  113. int BzrSrfWriteToFile2(CagdSrfStruct *Srfs, FILE *f, int Indent, char *Comment,
  114.                                 char **ErrStr)
  115. {
  116.     int i, j, MaxCoord;
  117.  
  118.     if (Comment != NULL) {
  119.     _CagdFprintf(f, Indent, "#\n");
  120.     _CagdFprintf(f, Indent, "# cagd_lib - bezier srf(s) dump.\n");
  121.     _CagdFprintf(f, Indent, "#\n");
  122.     _CagdFprintf(f, Indent, "# %s\n", Comment);
  123.     _CagdFprintf(f, Indent, "#\n");
  124.     }
  125.  
  126.     *ErrStr = NULL;
  127.  
  128.     while (Srfs) {
  129.     MaxCoord = CAGD_NUM_OF_PT_COORD(Srfs -> PType);
  130.  
  131.     if (Srfs -> GType != CAGD_SBEZIER_TYPE) {
  132.         *ErrStr = "Given surface(s) is (are) not BEZIER surface(s)";
  133.         break;
  134.     }
  135.     _CagdFprintf(f, Indent, "[SURFACE BEZIER %d %d %c%c\n",
  136.         Srfs -> ULength, Srfs -> VLength,
  137.         CAGD_IS_RATIONAL_PT(Srfs -> PType) ? 'P' : 'E',
  138.         MaxCoord + '0');
  139.     Indent += 4;
  140.  
  141.     for (i = 0; i < Srfs -> VLength * Srfs -> ULength; i++) {
  142.         if (i && i % Srfs -> ULength == 0)
  143.         _CagdFprintf(f, 0, "\n");    /* Put empty lines between raws. */
  144.  
  145.         _CagdFprintf(f, Indent, "[");
  146.         if (CAGD_IS_RATIONAL_PT(Srfs -> PType))
  147.         _CagdFprintf(f, 0, "%s ", _CagdReal2Str(Srfs -> Points[0][i]));
  148.         for (j = 1; j <= MaxCoord; j++) {
  149.         _CagdFprintf(f, 0, "%s", _CagdReal2Str(Srfs -> Points[j][i]));
  150.         if (j < MaxCoord) _CagdFprintf(f, 0, " ");
  151.         }
  152.         _CagdFprintf(f, 0, "]\n");
  153.     }
  154.  
  155.     Indent -= 4;
  156.     _CagdFprintf(f, Indent, "]\n\n");
  157.  
  158.     Srfs = Srfs -> Pnext;
  159.     }
  160.  
  161.     return *ErrStr == NULL;
  162. }
  163.